package df.bean.interfacefile;

import df.bean.db.conn.DBConnection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import df.bean.db.conn.DBConn;
import df.bean.db.table.TRN_Error;
import df.bean.obj.util.JDate;
/**
 *
 * @author arxim
 */
public class ImportTransactionBean extends InterfaceTextFileBean {
    private ResultSet rs;
    private Statement stm;
    
    @Override
    public boolean insertData(String fn, DBConnection d) {
        boolean status = true;
        ArrayList a = null;
        String[] sub_data = null;
        String temp = "";
        String message = "";
        int insert_count = 0;
        String errorField = "";
        
        try {
            setConn(d);//connect database
            setFileName(fn);//set filename read
            copyDataFile();//copy data from file to superclass arraylist member
            a = getData();//copy data from superclass arraylist member
            stm = getConn().getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stm.executeQuery("SELECT * FROM INT_HIS_BILL");
            for(int i = 0; i<a.size(); i++){
                if(i>0){
                    this.setMessage("File Transaction Error Line : "+i);
                    temp = (String)a.get(i);
                    //System.out.println(temp);
                    
                    //We change "^" to "@" after import to database and split by "@" instead
                    sub_data = temp.split("[@]");
                    
                    //System.out.println("Size of date : "+sub_data.length);
                    try{
                        rs.moveToInsertRow();
                        message = "Invoice No = "+sub_data[2]+" Invoice Date = "+sub_data[3]
                                  +" Line No = "+sub_data[33].trim()+":"
                                  +new String(sub_data[6].trim().getBytes(),"TIS-620");
                        this.setMessage(message);
                        errorField="HOSPITAL_CODE";
                        rs.updateString("HOSPITAL_CODE",sub_data[0].trim()); 
                        errorField="EPISODE_TYPE";
                        rs.updateString("EPISODE_TYPE",sub_data[1].trim());
                        errorField="BILL_NO";
                        rs.updateString("BILL_NO",sub_data[2].trim());
                        errorField="BILL_DATE";
                        rs.updateString("BILL_DATE",sub_data[3].trim());
                        errorField="RECEIPT_TYPE_CODE";
                        rs.updateString("RECEIPT_TYPE_CODE",sub_data[4].trim());
                        errorField="TRANSACTION_TYPE";
                        if(sub_data[4].trim().equals("AR")){
                            rs.updateString("TRANSACTION_TYPE","INV");
                        }else{
                            rs.updateString("TRANSACTION_TYPE","REV");
                        }
                        errorField="HN_NO";
                        rs.updateString("HN_NO", sub_data[5].trim());
                        errorField="PATIENT_NAME";
                        rs.updateString("PATIENT_NAME",new String(sub_data[6].trim().getBytes(),"TIS-620"));
                        errorField="EPISODE_NO";
                        rs.updateString("EPISODE_NO",sub_data[7].trim());
                        errorField="PAYOR_CODE";
                        rs.updateString("PAYOR_CODE",sub_data[8].trim());
                        errorField="PAYOR_NAME";
                        rs.updateString("PAYOR_NAME",new String(sub_data[9].trim().getBytes(),"TIS-620"));
                        errorField="PAYOR_CATEGORY_CODE";
                        rs.updateString("PAYOR_CATEGORY_CODE",sub_data[10].trim());
                        errorField="PAYOR_CATEGORY_DESC";
                        rs.updateString("PAYOR_CATEGORY_DESC",new String(sub_data[11].trim().getBytes(),"TIS-620"));
                        errorField="ADMISSION_TYPE_CODE";
                        if(sub_data[12].trim().equals("I")){
                            rs.updateString("ADMISSION_TYPE_CODE",sub_data[12].trim());
                        }else{
                            rs.updateString("ADMISSION_TYPE_CODE","O");
                        }
                        errorField="ORDER_ITEM_CODE";
                        rs.updateString("ORDER_ITEM_CODE",sub_data[13].trim());
                        errorField="ORDER_ITEM_DESCRIPTION";
                        rs.updateString("ORDER_ITEM_DESCRIPTION",new String(sub_data[14].trim().getBytes(),"TIS-620"));
                        errorField="DOCTOR_PROFILE_CODE";
                        rs.updateString("DOCTOR_PROFILE_CODE",sub_data[15].trim());
                        errorField="DOCTOR_PROFILE_NAME";
                        rs.updateString("DOCTOR_PROFILE_NAME",new String(sub_data[16].trim().getBytes(),"TIS-620"));
                        errorField="DOCTOR_CODE";
                        if(sub_data[17].length()<2){
                            rs.updateString("DOCTOR_CODE","99999999");
                        }else{
                            rs.updateString("DOCTOR_CODE",sub_data[17].trim());
                        }
                        errorField="DOCTOR_NAME";
                        rs.updateString("DOCTOR_NAME",new String(sub_data[18].trim().getBytes(),"TIS-620"));
                        errorField="AMOUNT_BEF_DISCOUNT";
                        rs.updateDouble("AMOUNT_BEF_DISCOUNT",Double.parseDouble(""+sub_data[19].trim()));
                        errorField="AMOUNT_OF_DISCOUNT";
                        rs.updateDouble("AMOUNT_OF_DISCOUNT",Double.parseDouble(""+sub_data[20].trim()));
                        errorField="ORDERED_DATE";
                        rs.updateString("ORDERED_DATE",sub_data[21].trim());
                        errorField="ORDERED_TIME";
                        rs.updateString("ORDERED_TIME",sub_data[22].trim());
                        errorField="NATIONALITY_CODE";
                        rs.updateString("NATIONALITY_CODE",sub_data[23].trim());
                        errorField="NATIONALITY_DESCRIPTION";
                        rs.updateString("NATIONALITY_DESCRIPTION",new String(sub_data[24].trim().getBytes(),"TIS-620"));
                        errorField="PATIENT_LOCATION_CODE";
                        rs.updateString("PATIENT_LOCATION_CODE",sub_data[25].trim());
                        errorField="PATIENT_LOCATION_DESC";
                        rs.updateString("PATIENT_LOCATION_DESC",new String(sub_data[26].trim().getBytes(),"TIS-620"));
                        errorField="PATIENT_LOCATION_DEPT_CODE";
                        rs.updateString("PATIENT_LOCATION_DEPT_CODE",sub_data[27].trim());
                        errorField="PATIENT_LOCATION_DEPT_DESC";
                        rs.updateString("PATIENT_LOCATION_DEPT_DESC",new String(sub_data[28].trim().getBytes(),"TIS-620"));
                        errorField="RECEIVING_LOCATION_CODE";
                        rs.updateString("RECEIVING_LOCATION_CODE",sub_data[29].trim());
                        errorField="RECEIVING_LOCATION_DESC";
                        rs.updateString("RECEIVING_LOCATION_DESC",new String(sub_data[30].trim().getBytes(),"TIS-620"));
                        errorField="RECEIVING_LOCATION_DEPT_CODE";
                        rs.updateString("RECEIVING_LOCATION_DEPT_CODE",sub_data[31].trim());
                        errorField="RECEIVING_LOCATION_DEPT_DESC";
                        rs.updateString("RECEIVING_LOCATION_DEPT_DESC",new String(sub_data[32].trim().getBytes(),"TIS-620"));
                        errorField="LINE_NO";
                        if(sub_data[33].trim().equals("") || sub_data[33].trim().equals("null")){
                            rs.updateString("LINE_NO",(""+JDate.getTimeInMillis()).substring(6)+"/"+i);
                        }else{
                            rs.updateString("LINE_NO",sub_data[33].trim());                            
                        }
                        
                        errorField="VERIFIED_DATE";
                        if(sub_data[34].trim().length() < 6){
                            rs.updateString("VERIFIED_DATE","");                                                        
                        }else{
                            rs.updateString("VERIFIED_DATE",""+sub_data[34].trim());                            
                        }
                        errorField="VERIFIED_TIME";
                        rs.updateString("VERIFIED_TIME",sub_data[35].trim());
                        errorField="BILL_TOTAL_AMOUNT";
                        rs.updateDouble("BILL_TOTAL_AMOUNT",Double.parseDouble(""+sub_data[36].trim()));
                        errorField="TRANSACTION_DATE";
                        if(this.getTransactionDate().length()< 2){
                        	rs.updateString("TRANSACTION_DATE",sub_data[3].trim());                        	
                        }else{
                        	rs.updateString("TRANSACTION_DATE",this.getTransactionDate());
                        }
                        errorField="PRIVATE_DOCTOR";
                        rs.updateString("PRIVATE_DOCTOR",sub_data[37].trim());
                        errorField="FIRST_LOCATION_CODE";
                        rs.updateString("FIRST_LOCATION_CODE",sub_data[38].trim());
                        errorField="FIRST_LOCATION_DESCRIPTION";
                        rs.updateString("FIRST_LOCATION_DESCRIPTION",sub_data[39].trim());
                        errorField="FIRST_DEPARTMENT_CODE";
                        rs.updateString("FIRST_DEPARTMENT_CODE",sub_data[40].trim());
                        errorField="FIRST_DEPARTMENT_DESCRIPTION";
                        rs.updateString("FIRST_DEPARTMENT_DESCRIPTION",sub_data[41].trim());
                        errorField="TEMP_LOCATION_CODE";
                        rs.updateString("TEMP_LOCATION_CODE",sub_data[42].trim());
                        errorField="TEMP_LOCATION_DESCRIPTION";
                        rs.updateString("TEMP_LOCATION_DESCRIPTION",sub_data[43].trim());
                        errorField="TEMP_DEPARTMENT_CODE";
                        rs.updateString("TEMP_DEPARTMENT_CODE",sub_data[44].trim());
                        errorField="TEMP_DEPARTMENT_DESCRIPTION";
                        rs.updateString("TEMP_DEPARTMENT_DESCRIPTION",sub_data[45].trim());
                        errorField="DERIVED_FEE_RULE";
                        rs.updateString("DERIVED_FEE_RULE",sub_data[46].trim());
                        if (sub_data[47].length()> 199) { 
                        	sub_data[47] = sub_data[47].substring(0, 200);
                        }
                        errorField="NOTE";
                        rs.updateString("NOTE",new String(sub_data[47].trim().getBytes(),"TIS-620"));
                        rs.insertRow();
                        insert_count++;
                    }catch(Exception e){
                    	System.out.println("Import Transaction Error : "+errorField+"<>"+message+" : "+e);
                    	TRN_Error.writeLogError(this.getConn().getConnection(), "InterfaceTransaction", this.getMessage(), e.toString(), message, "", sub_data[0].trim(), this.getUserName());
                    }
                }
                
            }
            if(insert_count < a.size()-1){
                setMessage("Error : "+ ""+(a.size()- (insert_count+1))+"/"+(a.size()-1)+" records complete : "+insert_count+" records.");
            }else{
                setMessage("Complete "+(a.size()-1)+"/"+insert_count+" records.");
            }
            setBillDate(sub_data[3].trim());
            status = true;
            this.getConn().commitTrans();
            rs.close();
        } catch (Exception f) {
        	System.out.println("Import Transaction : "+f);
            setInfo(f.toString());
            status = false;
            this.getConn().rollBackTrans();
        }
        finally {
            try {
                    if (rs != null) { 
                    rs.close();
                    rs = null;
                }
                if (stm != null) {
                    stm.close();
                    stm = null;
                }
            }
            catch (Exception ex)  {
                System.out.println(ex);
            }
            
        }
        return status;
    }

    @Override
    public boolean exportData(String fn, String hp, String type, String year, String month, DBConn d, String path) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
